import traceback

import numpy as np
import torch
from PIL import Image


class Monochrome:
    RETURN_TYPES = ("IMAGE",)
    # RETURN_NAMES = ("image_output_name",)

    FUNCTION = "image_creator"

    # OUTPUT_NODE = False

    CATEGORY = "Monochrome"

    @classmethod
    def INPUT_TYPES(cls):
        return {
            "required": {
                "image": ("IMAGE",),
            },
        }

    @staticmethod
    def image_creator(image):

        try:
            img = None
            for batch_number, image in enumerate(image):
                i = 255.0 * image.cpu().numpy()
                img = Image.fromarray(np.clip(i, 0, 255).astype(np.uint8))
            bw_image = img.convert('L')
            pil_image = np.array(bw_image).astype(np.float32) / 255.0
            image = torch.from_numpy(pil_image)[None,]
        except Exception as e:
            print(e, traceback.format_exc())
        return (image,)
